package com.chengyu.core;

import com.github.houbb.sensitive.word.api.IWordAllow;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 自定义非敏感词
 * 注意每一行为一个非敏感词，单行不能只包括空格，否则，也会把空格识别为非敏感词
 */
@Component
@Slf4j
public class MyWordAllow implements IWordAllow {

    @Value("classpath:myAllowWords.txt")
    private Resource resourceAllow;

    @Override
    public List<String> allow() {
        List<String> allowWords = new ArrayList<>();

        try (BufferedReader reader = new BufferedReader(new InputStreamReader(resourceAllow.getInputStream(), StandardCharsets.UTF_8))) {
            allowWords = reader.lines().collect(Collectors.toList());
            log.info("1============读取有效词语个数：{}",allowWords.size());
        }catch (Exception e){
            throw new RuntimeException(e);
        }
        return allowWords;
    }
}
